import 'package:flutter/material.dart';
import 'package:flutter_demo/utils/net.dart';
import 'package:fluttertoast/fluttertoast.dart';

class MinePage extends StatefulWidget {
  const MinePage({super.key});

  @override
  State<MinePage> createState() => _MinePageState();
}

class _MinePageState extends State<MinePage> {
  Map user = {'avatar': '', 'name': '', 'number': '', 'phone': ''};
  Map reportInfo = {
    'completedAmounts': 18,
    'taskAmounts': 30,
    'transportMileage': 5464,
  };
  @override
  void initState() {
    super.initState();
    getUserInfo();
    getTaskInfo();
  }

  getUserInfo() async {
    try {
      var result = await http.get('/driver/users', {});
      if (result['code'] == 200) {
        setState(() {
          user = result['data'];
        });
      }
    } catch (e) {
      Fluttertoast.showToast(msg: e.toString());
    }
  }

  getTaskInfo() async {
    Map<String, String> data = {};
    data['month'] = '8';
    data['year'] = '2025';
    var result = await http.get('/driver/users/taskReport', data);
    if (result['code'] == 200) {
      setState(() {
        reportInfo = result['data'];
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        child: Column(
          children: [
            Stack(
              alignment: AlignmentDirectional.topCenter,
              children: [
                // 背景
                Image.asset(
                  'assets/images/bg_page_my.jpg',
                  width: MediaQuery.of(context).size.width,
                  fit: BoxFit.contain,
                ),
                //前面的信息
                Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    SizedBox(height: 100),
                    CircleAvatar(
                      radius: 40,
                      child: user['avatar'].isNotEmpty
                          ? Image.network(user['avatar'], width: 80, height: 80)
                          : Image.asset(
                              'assets/images/background.png',
                              width: 80,
                              height: 80,
                            ),
                    ),
                    SizedBox(height: 10),
                    Text(
                      user['name'].isNotEmpty ? user['name'] : '',
                      style: TextStyle(color: Colors.white),
                    ),
                    SizedBox(height: 10),
                    Text(
                      '司机编号：${user['number']}',
                      style: TextStyle(color: Colors.white),
                    ),
                    SizedBox(height: 10),
                    Text(
                      '手机号：${user['phone']}',
                      style: TextStyle(color: Colors.white),
                    ),
                    SizedBox(height: 10),
                  ],
                ),
              ],
            ),
            Positioned(
              top: -500,

              child: Column(
                children: [
                  Padding(
                    padding: EdgeInsets.all(20),
                    child: Container(
                      decoration: BoxDecoration(
                        color: Colors.white,
                        borderRadius: BorderRadius.circular(10),
                      ),
                      child: Padding(
                        padding: EdgeInsets.all(10),
                        child: Column(
                          children: [
                            SizedBox(height: 10),
                            Text('--本月任务--'),
                            SizedBox(height: 10),
                            Row(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              children: [
                                Column(
                                  children: [
                                    Text('${reportInfo['taskAmounts']}'),
                                    SizedBox(height: 10),
                                    Text('任务总数'),
                                  ],
                                ),
                                Column(
                                  children: [
                                    Text('${reportInfo['completedAmounts']}'),
                                    SizedBox(height: 10),
                                    Text('完成任务量'),
                                  ],
                                ),
                                Column(
                                  children: [
                                    Text('${reportInfo['transportMileage']}'),
                                    SizedBox(height: 10),
                                    Text('运输里程（km）'),
                                  ],
                                ),
                              ],
                            ),
                          ],
                        ),
                      ),
                    ),
                  ),
                  Padding(
                    padding: EdgeInsets.all(10),
                    child: Container(
                      decoration: BoxDecoration(
                        color: Colors.white,
                        borderRadius: BorderRadius.circular(10),
                      ),
                      child: Padding(
                        padding: EdgeInsets.all(10),
                        child: Column(
                          children: [
                            Row(
                              children: [
                                Text('车辆信息', style: TextStyle(fontSize: 20)),
                                Expanded(child: Spacer()),
                                Image.asset(
                                  'assets/images/icon_arrow_right.png',
                                  width: 20,
                                  height: 20,
                                ),
                              ],
                            ),
                            SizedBox(height: 10),
                            Divider(
                              height: 0.5,
                              color: const Color.fromARGB(255, 192, 189, 189),
                            ),
                            SizedBox(height: 10),
                            Row(
                              children: [
                                Text('任务数据', style: TextStyle(fontSize: 20)),
                                Expanded(child: Spacer()),
                                Image.asset(
                                  'assets/images/icon_arrow_right.png',
                                  width: 20,
                                  height: 20,
                                ),
                              ],
                            ),
                            SizedBox(height: 10),
                            Divider(
                              height: 0.5,
                              color: const Color.fromARGB(255, 192, 189, 189),
                            ),
                            SizedBox(height: 10),
                            GestureDetector(
                              onTap: () {
                                Navigator.pushNamed(context, '/settings');
                              },
                              child: Row(
                                children: [
                                  Text('系统设置', style: TextStyle(fontSize: 20)),
                                  Expanded(child: Spacer()),
                                  Image.asset(
                                    'assets/images/icon_arrow_right.png',
                                    width: 20,
                                    height: 20,
                                  ),
                                ],
                              ),
                            ),
                          ],
                        ),
                      ),
                    ),
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}
